<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/mitglieder.php";
require_once $basedir."/inc/adress.php";
require_once $basedir."/inc/bankverbindung.php";
$mitglied = new Mitglieder();
if(isset($_REQUEST['id'])) {
	$mitglied = new Mitglieder($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
	$mitglied->setAdresseId($_REQUEST['adresse_id']);
	$mitglied->setBankId($_REQUEST['bank_id']);
	$mitglied->setEingetretenAm($_REQUEST['eingetreten_am']);
	$mitglied->setGekuendigtAm($_REQUEST['gekuendigt_am']);
	$mitglied->save();
}
$adress = $mitglied->getAdress();
$bank = $mitglied->getBank();
?>
<script language="JavaScript" type="text/javascript">
    $(function() {
	    $('#nurMitglieder').click(function(event) {
	    	var params = [
	    		'page=<?php echo $menuid; ?>',
	    		'onlyMembers=' + $('#nurMitglieder').is(':checked')
	    	];
	    	window.location.href = "http://" + window.location.host + window.location.pathname + "?" + params.join('&');
	    });
        $( "#stammdaten_mitglied_add_adresse_id" ).autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_adress',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	$('input[name=adresse_id]').attr('value', ui.item.id);
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.vorname + " " + item.nachname + "<br />"
        			+ item.strasse + " " + item.hausnummer + item.zusatz_hausnummer + "<br />"
        			+ item.plz + " " + item.ort + "</a>";
        	item.value = item.vorname + " " + item.nachname;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
        $( "#stammdaten_mitglied_add_bank_id" ).autocomplete({
            source: function(request, response) {
            	var data = {
            		'value': request.term,
            		'page': 'stammdaten_bank',
            		'action': 'search'
            	};
            	$.ajax({
            		'url': "json.php",
            		'data': data,
            		'type':'post',
            		'dataType': 'json',
            		'success': function(data) {
            			response(data.items);
            		}
            	})
            },
            minLength: 2,
            select: function(event, ui) {
            	$('input[name=bank_id]').attr('value', ui.item.id);
            }
        })
        .data("ui-autocomplete")._renderItem = function(ul, item) {
        	item.label = "<a>" + item.name + "<br />"
        			+ "BLZ: " + item.blz + "<br />"
        			+ "Kontonr: " + item.kontonummer + "</a>";
        	item.value = item.name;
        	return $("<li></li>")
        		.data("item.autocomplete", item)
        		.append(item.label)
        		.appendTo(ul);
        };
    });
</script>
<?php
if($action=='edit') {
	$action = 'add';
}
if($action=='add') {
	$form_display = "block";
} else {
	$form_display = "none";
}
?>
<form id='stammdaten_mitglied_add_form' method="post" action="/index.php" style='display:<?php echo $form_display; ?>'>
	<input type="hidden" name="page" value="<?php echo $menuid;?>" />
	<input type="hidden" name="id" value="<?php echo $mitglied->getId(); ?>" />
    <div>
    	<div class='inline-block label'>Adresse:</div>
    	<div class='inline-block'>
	        <input name="adresse_id" type="hidden" value="<?php echo $mitglied->getAdresseId(); ?>" />
	        <input id="stammdaten_mitglied_add_adresse_id" type="text" name="adresse" maxlength="101" value="<?php echo ($adress->getVorname()!=""?$adress->getVorname()." ":"").$adress->getNachname();?>" /><br />
	        <span>
	        	<?php echo $adress->getStrasse()." ".$adress->getHausnummer().$adress->getZusatzHausnummer(); ?><br />
	        	<?php echo $adress->getPlz()." ".$adress->getOrt(); ?>
	        </span>
    	</div>
    </div>
    <div>
    	<div class="inline-block label">Bank:</div>
    	<div class='inline-block'>
	        <input type="hidden" name="bank_id" value="<?php echo $mitglied->getBankId();?>" />
	        <input id="stammdaten_mitglied_add_bank_id" type="number" name="bank" maxlength="100" value="<?php echo $bank->getName(); ?>" /><br />
	        <span>
	        	<?php echo "BLZ: ".$bank->getBlz(); ?><br />
	        	<?php echo "Kontonr.: ".$bank->getKontonummer(); ?>
	        </span>
    	</div>
    </div>
    <div>
        <div class='inline-block label'>Eingetretten am:</div>
        <input type="date" name="eingetreten_am" maxlength="10" value="<?php echo $mitglied->getEingetretenAm();?>" />
    </div>
    <div>
        <div class="inline-block label">Ausgetretten am:</div>
        <input type="date" name="gekuendigt_am" maxlength="10" value="<?php echo $mitglied->getGekuendigtAm() ?>"/>
    </div>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
<?php
if($action!="add") {
	$onlyMembers=true;
	if(isset($_REQUEST['onlyMembers'])) {
		$onlyMembers = $_REQUEST['onlyMembers']=='true'?true:false;
	}
	$mitglieder = Mitglieder::getAll($onlyMembers);
?>
<div>
    <a href='?page=stammdaten_mitglieder&action=add'>Neues Mitglied</a> ||
    <input type='checkbox' value='' name='nurMitglieder' id='nurMitglieder' <?php echo $onlyMembers?'checked':''; ?>></input> Nur Mitglieder
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
        	<th></th>
            <th>Mitglied</th>
            <th>Bankverbindung</th>
            <th>Eingetreten</th>
            <th>Ausgeteten</th>
            <th></th>
        </tr>
<?php
if($mitglieder!=NULL) {
	for($i=0;$i<count($mitglieder);$i++) {
		$value = $mitglieder[$i];
		$adress = new Adresse($value->getAdresseId());
		$bank = new Bankverbindung($value->getBankId());
		$css_class = "even";
		if($i%2==1) {
			$css_class = "odd";
		}
?>
		<tr class="<?php echo $css_class; ?>">
			<td><?php echo ($i+1); ?></td>
			<td><?php echo str_replace("\n", "<br />", $adress->getLabel());?></td>
			<td><?php echo str_replace("\n", "<br />", $bank->getLabel());?></td>
			<td><?php echo $value->getEingetretenAm();?></td>
			<td><?php echo $value->getGekuendigtAm();?></td>
			<td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $value->getId();?>">edit</a></td>
		</tr>
<?php
	}
}
?>
    </table>
</div>
<?php    
}
?>